package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.mapper.DemoMapper;
import com.neuedu.pojo.Demo;
import com.neuedu.pojo.Result;
import com.neuedu.service.DemoService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * @author 屈博
 * @email qubome@aliyun.com
 * @Date 2024/6/4 21:29
 */
@Service
public class DemoServiceImpl extends ServiceImpl<DemoMapper, Demo>
        implements DemoService {
    @Resource
    private DemoMapper demoMapper;
    @Override
    public Result<List<Demo>> getPage(Integer currentPage, Integer pageSize, String name) {
        // 设置分页参数
        Page<Demo> page = new Page<>(currentPage,pageSize);
        // 设置条件查询的构造器
        LambdaQueryWrapper<Demo> wrapper = new LambdaQueryWrapper<>();
        // 若条件为null，这个相当于全部查询
        wrapper.like(StringUtils.isNotBlank(name),Demo::getName,name);
        this.demoMapper.selectPage(page,wrapper);
        if (page.getRecords().size()>0){
            return Result.success(page.getRecords(),"查询成功",page.getTotal());
        }else{
            return Result.fail("查询失败");
        }
    }

    @Override
    public Result<Void> addDemo(Demo demo) {
        if (this.demoMapper.insert(demo)>0){
            return Result.success("添加成功");
        }else{
            return Result.fail("添加失败");
        }
    }

    @Override
    public Result<Void> updateDemo(Demo demo) {
        if (this.demoMapper.updateById(demo)>0){
            return Result.success("修改成功");
        }else{
            return Result.fail("修改失败");
        }
    }

    @Override
    public Result<Void> deleteDemo(Integer id) {
        if (this.demoMapper.deleteById(id)>0){
            return Result.success("删除成功");
        }else{
            return Result.fail("删除失败");
        }
    }
}
